package com.util;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;

public final class JsonParser {

	private static final Gson GSON;

	static {
		GsonBuilder gs = new GsonBuilder();
		gs.serializeNulls();
		gs.disableHtmlEscaping();
		gs.setLongSerializationPolicy(LongSerializationPolicy.STRING);
		GSON = gs.create();
	}

	private JsonParser() {
	}

	public static <T> String parseToJSon(T obj) {
		try {
			return GSON.toJson(obj);
		} catch (Exception e) {
			throw new IllegalStateException(String.format("Couln't parse object '%s' to json", obj), e);
		}
	}

	public static <T> T parseFromJSon(String json, Type returnType) {
		try {
			return GSON.fromJson(json, returnType);
		} catch (Exception e) {

			throw new IllegalStateException(String.format("Couln't parse json '%s' to class '%s'", json, returnType), e);
		}
	}
}
